<title>banner编辑</title>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">编辑banner</div>
                <div class="layui-card-body">

                   <div class="layui-form-item">
                       <div class="layui-col-md12">
                           <input type="text" name="title" placeholder="请输入链接" autocomplete="off" class="layui-input">
                       </div>

                   </div>
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="test-upload-normal">上传图片</button>
                        <div class="layui-upload-list">
                            <img class="layui-upload-img" id="test-upload-normal-img">
                            <p id="test-upload-demoText"></p>
                        </div>
                    </div>
                </div>
            </div>
        </div>


    </div>
</div>
<script src="//cdn.bootcss.com/plupload/2.1.8/plupload.full.min.js"></script>
<script src="//lib.eqh5.com/qiniu-js/1.0.17.1/qiniu.min.js"></script>
<script>
    layui.use(['admin', 'upload'], function(){
        var $ = layui.jquery
            ,upload = layui.upload;

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test-upload-normal'
            ,url: '/upload/'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#test-upload-normal-img').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code > 0){
                    return layer.msg('上传失败');
                }
                //上传成功
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#test-upload-demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });
        $.ajax({
            type: "POST",
            url: "/api/uptokens/",
            dataType: "json",
            success: function(data) {
                if (data.code != 200) return layer.close(index), layer.msg('无效token', {
                    icon: 5
                });
                $("#upload-progress").css({"background":"url(/Public/default/images/loading.jpg) no-repeat","background-size":"0% 100%"});
                var uploader = Qiniu.uploader({
                    runtimes: 'html5,flash,html4',
                    browse_button: 'pickfiles',
                    uptoken_url: '/api/uptokens/',
                    uptoken: data.token,
                    unique_names: false,
                    save_key: false,
                    domain: 'http://qiniu-plupload.qiniudn.com/',
                    get_new_uptoken: false,
                    container: 'container',
                    max_file_size: '1mb',
                    max_retries: 3,
                    dragdrop: !0,
                    drop_element: 'container',
                    chunk_size: '1mb',
                    filters: {
                        mime_types: [{
                            title: "Image files",
                            extensions: "jpg,gif,png,jpeg"
                        }]
                    },
                    auto_start: !0,
                    init: {
                        'FilesAdded': function(up, files) {
                            $("#upload-progress").text("上传中......");
                            plupload.each(files, function(file) {});
                        },
                        'BeforeUpload': function(up, file) {},
                        'UploadProgress': function(up, file) {
                            $("#upload-progress").css({
                                "background-size": up.total.percent + "% 100%"
                            })

                        },
                        'FileUploaded': function(up, file, info) {
                            var z = JSON.parse(info);
                            var img_domain = "http://view20161106.wedlaa.com/"
                            var imageView = "?imageView2/0/w/756"; //限定长边
                            layui.focusInsert(editor[0], 'img[' + img_domain + z.key + imageView + '] \n');
                        },
                        'Error': function(up, err, errTip) {
                            layer.msg('上传失败', {
                                icon: 5
                            })
                        },
                        'UploadComplete': function(data) {
                            $("#upload-progress").text("上传文件");
                            layer.close(index);

                        },
                        'Key': function(up, file) {
                            //var key = "";
                            //return key
                        }
                    }
                });
            }
        });




    });
</script>